package cn.edu.gzhu.workOrderManagement.controller;

import cn.edu.gzhu.workOrderManagement.constants.MessageConstant;
import cn.edu.gzhu.workOrderManagement.pojo.dto.team.TeamCreateDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.team.TeamDeleteDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.team.TeamListDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.team.TeamUpdateDto;
import cn.edu.gzhu.workOrderManagement.pojo.entity.Team;
import cn.edu.gzhu.workOrderManagement.result.Result;
import cn.edu.gzhu.workOrderManagement.service.TeamService;
import cn.edu.gzhu.workOrderManagement.util.MyBeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/team")
@Tag(name = "组别接口")
public class TeamController {
    @Autowired
    TeamService teamService;
    @PostMapping("/create")
    public Result createTeam(@RequestHeader String token,@RequestBody @Validated TeamCreateDto teamCreateDto) {
        Boolean team = teamService.createTeam(teamCreateDto);
        if (!team) {
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }
    @GetMapping("/list")
    public Result listTeam(@RequestHeader String token,@Validated TeamListDto teamListDto) {
        //把“” 字符串设置为null
        MyBeanUtil.setEmptyStringsToNull(teamListDto);
        IPage<Team> teamIPage = teamService.listTeam(teamListDto);
        return Result.success(teamIPage);
    }
    @PostMapping("/delete")
    public Result deleteTeam(@RequestHeader String token,@RequestBody @Validated TeamDeleteDto teamDeleteDto) {
        Boolean b = teamService.deleteTeam(teamDeleteDto);
        if (!b) {
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }
    @PostMapping("/update")
    public Result updateTeam(@RequestHeader String token,@RequestBody @Validated TeamUpdateDto teamUpdateDto) {
        MyBeanUtil.setEmptyStringsToNull(teamUpdateDto);
        Boolean b = teamService.updateTeam(teamUpdateDto);
        if (!b) {
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }

}
